home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / system-config-printer / authconn.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-10-12  |  14.0 KB  |  391 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import threading
  5. import cups
  6. import cupspk
  7. import gobject
  8. import gtk
  9. from errordialogs import *
  10. from debug import *
  11.  
  12. _ = lambda x: x
  13.  
  14. N_ = lambda x: x
  15.  
  16. def set_gettext_function(fn):
  17.     global _
  18.     _ = fn
  19.  
  20.  
  21. class AuthDialog(gtk.Dialog):
  22.     AUTH_FIELD = {
  23.         'username': N_('Username:'),
  24.         'password': N_('Password:'),
  25.         'domain': N_('Domain:') }
  26.     
  27.     def __init__(self, title = None, parent = None, flags = gtk.DIALOG_MODAL | gtk.DIALOG_NO_SEPARATOR, buttons = (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, gtk.STOCK_OK, gtk.RESPONSE_OK), auth_info_required = [
  28.         'username',
  29.         'password'], allow_remember = False):
  30.         if title == None:
  31.             title = _('Authentication')
  32.         
  33.         gtk.Dialog.__init__(self, title, parent, flags, buttons)
  34.         self.auth_info_required = auth_info_required
  35.         self.set_default_response(gtk.RESPONSE_OK)
  36.         self.set_border_width(6)
  37.         self.set_resizable(False)
  38.         hbox = gtk.HBox(False, 12)
  39.         hbox.set_border_width(6)
  40.         image = gtk.Image()
  41.         image.set_from_stock(gtk.STOCK_DIALOG_AUTHENTICATION, gtk.ICON_SIZE_DIALOG)
  42.         image.set_alignment(0, 0)
  43.         hbox.pack_start(image, False, False, 0)
  44.         vbox = gtk.VBox(False, 12)
  45.         self.prompt_label = gtk.Label()
  46.         vbox.pack_start(self.prompt_label, False, False, 0)
  47.         num_fields = len(auth_info_required)
  48.         table = gtk.Table(num_fields, 2)
  49.         table.set_row_spacings(6)
  50.         table.set_col_spacings(6)
  51.         self.field_entry = []
  52.         for i in range(num_fields):
  53.             field = auth_info_required[i]
  54.             label = gtk.Label(_(self.AUTH_FIELD.get(field, field)))
  55.             label.set_alignment(0, 0.5)
  56.             table.attach(label, 0, 1, i, i + 1)
  57.             entry = gtk.Entry()
  58.             entry.set_visibility(field != 'password')
  59.             table.attach(entry, 1, 2, i, i + 1, 0, 0)
  60.             self.field_entry.append(entry)
  61.         
  62.         self.field_entry[num_fields - 1].set_activates_default(True)
  63.         vbox.pack_start(table, False, False, 0)
  64.         hbox.pack_start(vbox, False, False, 0)
  65.         self.vbox.pack_start(hbox)
  66.         if allow_remember:
  67.             cb = gtk.CheckButton(_('Remember password'))
  68.             cb.set_active(False)
  69.             vbox.pack_start(cb)
  70.             self.remember_checkbox = cb
  71.         
  72.         self.vbox.show_all()
  73.  
  74.     
  75.     def set_prompt(self, prompt):
  76.         self.prompt_label.set_markup('<span weight="bold" size="larger">' + prompt + '</span>')
  77.         self.prompt_label.set_use_markup(True)
  78.         self.prompt_label.set_alignment(0, 0)
  79.         self.prompt_label.set_line_wrap(True)
  80.  
  81.     
  82.     def set_auth_info(self, auth_info):
  83.         for i in range(len(self.field_entry)):
  84.             self.field_entry[i].set_text(auth_info[i])
  85.         
  86.  
  87.     
  88.     def get_auth_info(self):
  89.         return map((lambda x: x.get_text()), self.field_entry)
  90.  
  91.     
  92.     def get_remember_password(self):
  93.         
  94.         try:
  95.             return self.remember_checkbox.get_active()
  96.         except AttributeError:
  97.             return False
  98.  
  99.  
  100.     
  101.     def field_grab_focus(self, field):
  102.         i = self.auth_info_required.index(field)
  103.         self.field_entry[i].grab_focus()
  104.  
  105.  
  106.  
  107. class Connection:
  108.     
  109.     def __init__(self, parent = None, try_as_root = True, lock = False, host = None, port = None, encryption = None):
  110.         if host != None:
  111.             cups.setServer(host)
  112.         
  113.         if port != None:
  114.             cups.setPort(port)
  115.         
  116.         if encryption != None:
  117.             cups.setEncryption(encryption)
  118.         
  119.         self._use_password = ''
  120.         self._parent = parent
  121.         self._try_as_root = try_as_root
  122.         self._use_user = cups.getUser()
  123.         self._server = cups.getServer()
  124.         self._port = cups.getPort()
  125.         self._encryption = cups.getEncryption()
  126.         self._connect()
  127.         self._prompt_allowed = True
  128.         self._operation_stack = []
  129.         self._lock = lock
  130.         self._gui_event = threading.Event()
  131.  
  132.     
  133.     def _begin_operation(self, operation):
  134.         self._operation_stack.append(operation)
  135.  
  136.     
  137.     def _end_operation(self):
  138.         self._operation_stack.pop()
  139.  
  140.     
  141.     def _get_prompt_allowed(self):
  142.         return self._prompt_allowed
  143.  
  144.     
  145.     def _set_prompt_allowed(self, allowed):
  146.         self._prompt_allowed = allowed
  147.  
  148.     
  149.     def _set_lock(self, whether):
  150.         self._lock = whether
  151.  
  152.     
  153.     def _connect(self):
  154.         cups.setUser(self._use_user)
  155.         if not self._server[0] == '/':
  156.             pass
  157.         self._use_pk = self._server == 'localhost'
  158.         if self._use_pk:
  159.             create_object = cupspk.Connection
  160.         else:
  161.             create_object = cups.Connection
  162.         self._connection = create_object(host = self._server, port = self._port, encryption = self._encryption)
  163.         if self._use_pk:
  164.             self._connection.set_parent(self._parent)
  165.         
  166.         self._user = self._use_user
  167.         debugprint('Connected as user %s' % self._user)
  168.         methodtype_lambda = type(self._connection.getPrinters)
  169.         methodtype_real = type(self._connection.addPrinter)
  170.         for fname in dir(self._connection):
  171.             if fname[0] == '_':
  172.                 continue
  173.             
  174.             fn = getattr(self._connection, fname)
  175.             if type(fn) not in [
  176.                 methodtype_lambda,
  177.                 methodtype_real]:
  178.                 continue
  179.             
  180.             setattr(self, fname, self._make_binding(fname, fn))
  181.         
  182.  
  183.     
  184.     def _make_binding(self, fname, fn):
  185.         return (lambda : self._authloop(fname, fn, *args, **kwds))
  186.  
  187.     
  188.     def _authloop(self, fname, fn, *args, **kwds):
  189.         self._passes = 0
  190.         c = self._connection
  191.         retry = False
  192.         while retry or self._perform_authentication() != 0:
  193.             if c != self._connection:
  194.                 fn = getattr(self._connection, fname)
  195.                 c = self._connection
  196.             
  197.             cups.setUser(self._use_user)
  198.             
  199.             try:
  200.                 result = fn.__call__(*args, **kwds)
  201.                 if fname == 'adminGetServerSettings':
  202.                     if result == { }:
  203.                         raise cups.IPPError(cups.IPP_NOT_AUTHORIZED, '')
  204.                     result == { }
  205.                 
  206.             continue
  207.             except cups.IPPError:
  208.                 (e, m) = None
  209.                 if self._use_pk and m == 'pkcancel':
  210.                     title = 'Unauthorized request (%s)' % fname
  211.                     text = 'You are not authorized for the requested action'
  212.                     show_info_dialog(title, text, None)
  213.                     raise cups.IPPError(0, _('Operation canceled'))
  214.                 m == 'pkcancel'
  215.                 if not (self._cancel):
  216.                     pass
  217.                 None if e == cups.IPP_NOT_AUTHORIZED or e == cups.IPP_FORBIDDEN else e == cups.IPP_FORBIDDEN
  218.                 if not (self._cancel) and e == cups.IPP_SERVICE_UNAVAILABLE:
  219.                     if self._lock:
  220.                         self._gui_event.clear()
  221.                         gobject.timeout_add(1, self._ask_retry_server_error, m)
  222.                         self._gui_event.wait()
  223.                     else:
  224.                         self._ask_retry_server_error(m)
  225.                     if self._retry_response == gtk.RESPONSE_OK:
  226.                         debugprint('retrying operation...')
  227.                         retry = True
  228.                         self._passes -= 1
  229.                     else:
  230.                         self._cancel = True
  231.                         raise 
  232.                 self._retry_response == gtk.RESPONSE_OK
  233.                 if self._cancel and not (self._cannot_auth):
  234.                     raise cups.IPPError(0, _('Operation canceled'))
  235.                 not (self._cannot_auth)
  236.                 raise 
  237.                 continue
  238.                 except cups.HTTPError:
  239.                     (s,) = None
  240.                     if not (self._cancel):
  241.                         pass
  242.                     None if s == cups.HTTP_UNAUTHORIZED or s == cups.HTTP_FORBIDDEN else s == cups.HTTP_FORBIDDEN
  243.                     raise 
  244.                     continue
  245.                 
  246.                 None<EXCEPTION MATCH>cups.HTTPError
  247.             return result
  248.  
  249.  
  250.     
  251.     def _ask_retry_server_error(self, message):
  252.         if self._lock:
  253.             gtk.gdk.threads_enter()
  254.         
  255.         d = gtk.MessageDialog(self._parent, gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT, gtk.MESSAGE_ERROR, gtk.BUTTONS_NONE, _('CUPS server error (%s)') % self._operation_stack[0])
  256.         d.format_secondary_text(_("There was an error during the CUPS operation: '%s'." % message))
  257.         d.add_buttons(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, _('Retry'), gtk.RESPONSE_OK)
  258.         d.set_default_response(gtk.RESPONSE_OK)
  259.         if self._lock:
  260.             d.connect('response', self._on_retry_server_error_response)
  261.             gtk.gdk.threads_leave()
  262.         else:
  263.             self._retry_response = d.run()
  264.             d.destroy()
  265.  
  266.     
  267.     def _on_retry_server_error_response(self, dialog, response):
  268.         self._retry_response = response
  269.         dialog.destroy()
  270.         self._gui_event.set()
  271.  
  272.     
  273.     def _failed(self, forbidden = False):
  274.         self._has_failed = True
  275.         self._forbidden = forbidden
  276.  
  277.     
  278.     def _password_callback(self, prompt):
  279.         debugprint('Got password callback')
  280.         if self._cancel or self._auth_called:
  281.             return ''
  282.         self._auth_called = True
  283.         self._prompt = prompt
  284.         return self._use_password
  285.  
  286.     
  287.     def _perform_authentication(self):
  288.         self._passes += 1
  289.         debugprint('Authentication pass: %d' % self._passes)
  290.         if self._passes == 1:
  291.             self._has_failed = False
  292.             self._forbidden = False
  293.             self._auth_called = False
  294.             self._cancel = False
  295.             self._cannot_auth = False
  296.             self._dialog_shown = False
  297.             cups.setPasswordCB(self._password_callback)
  298.             debugprint('Authentication: password callback set')
  299.             return 1
  300.         debugprint('Forbidden: %s' % self._forbidden)
  301.         if not self._has_failed:
  302.             debugprint('Authentication: Operation successful')
  303.             return 0
  304.         self._has_failed = False
  305.         if self._passes >= 2:
  306.             pass
  307.         if not self._prompt_allowed:
  308.             debugprint('Authentication: prompting not allowed')
  309.             self._cancel = True
  310.             return 1
  311.         if not self._auth_called:
  312.             debugprint('Authentication: giving up')
  313.             self._cancel = True
  314.             self._cannot_auth = True
  315.             return 1
  316.         self._auth_called = False
  317.         if self._cancel:
  318.             debugprint('cancelled')
  319.             return -1
  320.         cups.setUser(self._use_user)
  321.         debugprint('Authentication: Reconnect')
  322.         self._connect()
  323.         return 1
  324.  
  325.     
  326.     def _show_not_authorized_dialog(self):
  327.         if self._lock:
  328.             gtk.gdk.threads_enter()
  329.         
  330.         d = gtk.MessageDialog(self._parent, gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT, gtk.MESSAGE_ERROR, gtk.BUTTONS_CLOSE)
  331.         d.set_title(_('Not authorized'))
  332.         d.set_markup('<span weight="bold" size="larger">' + _('Not authorized') + '</span>\n\n' + _('The password may be incorrect.'))
  333.         if self._lock:
  334.             d.connect('response', self._on_not_authorized_dialog_response)
  335.             gtk.gdk.threads_leave()
  336.         else:
  337.             d.run()
  338.             d.destroy()
  339.  
  340.     
  341.     def _on_not_authorized_dialog_response(self, dialog, response):
  342.         self._gui_event.set()
  343.         dialog.destroy()
  344.  
  345.     
  346.     def _perform_authentication_with_dialog(self):
  347.         if self._lock:
  348.             gtk.gdk.threads_enter()
  349.         
  350.         if len(self._operation_stack) > 0:
  351.             d = AuthDialog(title = _('Authentication (%s)') % self._operation_stack[0], parent = self._parent)
  352.         else:
  353.             d = AuthDialog(parent = self._parent)
  354.         d.set_prompt(self._prompt)
  355.         d.set_auth_info([
  356.             self._use_user,
  357.             ''])
  358.         d.field_grab_focus('password')
  359.         d.set_keep_above(True)
  360.         d.show_all()
  361.         d.show_now()
  362.         self._dialog_shown = True
  363.         if self._lock:
  364.             d.connect('response', self._on_authentication_response)
  365.             gtk.gdk.threads_leave()
  366.         else:
  367.             response = d.run()
  368.             self._on_authentication_response(d, response)
  369.  
  370.     
  371.     def _on_authentication_response(self, dialog, response):
  372.         (self._use_user, self._use_password) = dialog.get_auth_info()
  373.         dialog.destroy()
  374.         if response == gtk.RESPONSE_CANCEL or response == gtk.RESPONSE_DELETE_EVENT:
  375.             self._cancel = True
  376.         
  377.         if self._lock:
  378.             self._gui_event.set()
  379.         
  380.  
  381.  
  382. if __name__ == '__main__':
  383.     gtk.gdk.threads_init()
  384.     from timedops import TimedOperation
  385.     set_debugging(True)
  386.     c = TimedOperation(Connection, args = (None,)).run()
  387.     debugprint('Connected')
  388.     c._set_lock(True)
  389.     print TimedOperation(c.getFile, args = ('/admin/conf/cupsd.conf', '/dev/stdout')).run()
  390.  
  391.